﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mang24k99.Model.COMM;
using Mang24k99.Model.GOLD;
using Mang24k99.Business.GOLD;
using Castle.ActiveRecord;

namespace Mang24k99.Web.ProductQuote
{
    public partial class GoldBreed : System.Web.UI.Page
    {
        //产品归属类别：1-金商，2-银行
        protected string product_type = "1";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["procduct_type"] != null)
            {
                product_type = Request.QueryString["procduct_type"].ToString();
                if (product_type=="1")
                {
                    this.sort.Visible=false;
                }
            }
            if (!IsPostBack)
            {
                this.Panel2.Visible = false;
                BindGoldType();
                BindGridView();
            }
        }
        /// <summary>
        /// 绑定金品分类
        /// </summary>
        public void BindGoldType()
        {
            MKtCommCodeList[] codelist ;
            if (product_type == "1")
            {
                codelist = (MKtCommCodeList[])BKtGoldGoldbreed.SelectGoldTypeBySeller();
            }
            else
            {
                codelist = (MKtCommCodeList[])BKtGoldGoldbreed.SelectGoldTypeByBank();
            }
            this.ddlGoldType.DataSource = codelist;
            this.ddlGoldType.DataTextField = "CODE_CNAME";
            this.ddlGoldType.DataValueField="CODE_NO";
            this.ddlGoldType.DataBind();
        }
        /// <summary>
        /// 绑定gridview
        /// </summary>
        public void BindGridView()
        {
            int breedtype = Convert.ToInt32(this.ddlGoldType.SelectedValue);
            MKtGoldGoldbreed[] breedlist = (MKtGoldGoldbreed[])BKtGoldGoldbreed.SelectByType(breedtype,Convert.ToInt32(product_type));
            this.grdList.DataSource = breedlist;
            this.grdList.DataBind();
        }
        /// <summary>
        /// 点击新增品种按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            this.Panel2.Visible = true;
            this.Panel1.Visible = false;
            int breedno=(int)BKtGoldGoldbreed.SelectMaxNO()+1;
            this.txtBreedNO.Text =breedno.ToString();
        }
        /// <summary>
        /// 新增修改品种
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidatePage() == false)
            {
                return;
            }

            if (this.btnSave.Text=="增加")
            {
                if (NOValidate() == false)
                {
                    return;
                }
                if (NameValidate() == false)
                {
                    return;
                }
                MKtGoldGoldbreed goldbreed = new MKtGoldGoldbreed();
                goldbreed.BreedNO = Convert.ToInt32(this.txtBreedNO.Text.Trim());
                goldbreed.BreedName = this.txtBreedName.Text.Trim();
                MKtCommCodeList codelist = new MKtCommCodeList();
                codelist.CODE_NO = Convert.ToInt32(this.ddlGoldType.SelectedValue);
                goldbreed.MKtCommCodelist = codelist;
                if (this.ckbHidden.Checked==true)
                {
                    goldbreed.CloseShow = 1;
                }
                else
                {
                    goldbreed.CloseShow = 0;
                }
                
                goldbreed.ProductBelong = Convert.ToInt32(product_type);
                goldbreed.IsValidate = 1;
                goldbreed.InputTime = DateTime.Now;
                try
                {
                    BKtGoldGoldbreed.Create(goldbreed);
                    string strMsg = "添加成功！";
                    Common.MessageBox.Show(this.Page, strMsg);
                    PageClear();                    
                    BindGridView();
                }
                catch (System.Exception ex)
                {
                    throw;
                }
            }
            if (this.btnSave.Text=="更新")
            {
                if (Session["BreedName"]!=null)
                {
                    if (Session["BreedName"].ToString()!=this.txtBreedName.Text.Trim())
                    {
                        if (NameValidate()==false)
                        {
                            return;
                        }
                    }
                }
                else
                {
                    return;
                }
                
                using(new SessionScope())
                {
                    int breedno = Convert.ToInt32(this.txtBreedNO.Text.Trim());
                    MKtGoldGoldbreed goldbreed = BKtGoldGoldbreed.Find(breedno);
                    goldbreed.BreedName = this.txtBreedName.Text.Trim();
                    goldbreed.ModifyTime = DateTime.Now;
                    if (this.ckbHidden.Checked == true)
                    {
                        goldbreed.CloseShow = 1;
                        for (int i = 0; i < goldbreed.MKtGoldProduct.Count;i++ )
                        {
                            goldbreed.MKtGoldProduct[i].CloseShow = 1;
                        }
                    }
                    else
                    {
                        goldbreed.CloseShow = 0;
                        for (int i = 0; i < goldbreed.MKtGoldProduct.Count; i++)
                        {
                            goldbreed.MKtGoldProduct[i].CloseShow = 0;
                        }
                    }
                     try
                     {
                         MKtGoldGoldbreed.Update(goldbreed);
                         string strMsg = "修改成功！";
                         Common.MessageBox.Show(this.Page, strMsg);
                         Session["BreedName"] = "";
                         PageClear();
                         BindGridView();
                     }
                     catch (System.Exception ex)
                     {
                         throw;
                     }
                }
               
                
            }
            
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            PageClear();
        }
        /// <summary>
        /// 清空页面
        /// </summary>
        public  void PageClear()
        {
            int breedno = (int)BKtGoldGoldbreed.SelectMaxNO() + 1;
            this.txtBreedNO.Text = breedno.ToString();
            this.txtBreedName.Text = "";
            this.Panel1.Visible = true;
            this.Panel2.Visible = false;
            this.btnSave.Text = "增加";
            this.ckbHidden.Checked = false;
            this.txtBreedNO.ReadOnly = false;
        }
        /// <summary>
        /// 页面有效性验证
        /// </summary>
        /// <returns></returns>
        public bool ValidatePage()
        {
            if (this.txtBreedNO.Text.Trim()=="")
            {
                string strMsg = "代码不能为空！";
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            if (Common.PageValidate.IsNumber(this.txtBreedNO.Text)==false)
            {
                string strMsg = "代码格式不对，请输入数字！";
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            } 
            if (this.txtBreedName.Text.Trim()=="")
            {
                string strMsg = "代码名称不能为空！";
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            
            return true;
        }
        /// <summary>
        /// 判断代码是否重复
        /// </summary>
        /// <returns></returns>
        public bool NOValidate()
        {
            int breedno = Convert.ToInt32(this.txtBreedNO.Text.Trim());
            MKtGoldGoldbreed[] goldbreedlist = (MKtGoldGoldbreed[])BKtGoldGoldbreed.SelectByNO(breedno);
            if (goldbreedlist.Length > 0)
            {
                string strMsg = "代码(" + breedno + ")已经使用，请修改代码！"; ;
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            return true;
        }
        /// <summary>
        /// 判断品种名称是否重复
        /// </summary>
        /// <returns></returns>
        public bool NameValidate()
        {
            int breedtype = Convert.ToInt32(this.ddlGoldType.SelectedValue);
            string breedname = this.txtBreedName.Text.Trim();
            MKtGoldGoldbreed[] goldbreedlist = (MKtGoldGoldbreed[])BKtGoldGoldbreed.SelectByName(breedname, breedtype,Convert.ToInt32(product_type));
            if (goldbreedlist.Length > 0)
            {
                string strMsg = "品种名称(" + breedname + ")已经使用，请修改名称！"; ;
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            return true;
        }
        protected void ddlGoldType_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGridView();
            PageClear();
        }
        /// <summary>
        /// 删除记录，物理删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void grdList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int breedno = Convert.ToInt32(this.grdList.Rows[e.RowIndex].Cells[1].Text);
            MKtGoldGoldbreed goldbreed = (MKtGoldGoldbreed)BKtGoldGoldbreed.Find(breedno);
            try
            {
                MKtGoldGoldbreed.Delete(goldbreed);
                string strMsg = "删除成功！";
                Common.MessageBox.Show(this.Page, strMsg);
                PageClear();
                BindGridView();
            }
            catch (System.Exception ex)
            {
                string strMsg = "删除失败！";
                Common.MessageBox.Show(this.Page, strMsg);
                BindGridView();
            }
            
        }
        /// <summary>
        /// 选择一行进行编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.btnSave.Text = "更新";
            this.txtBreedNO.ReadOnly = true;
            int index = this.grdList.SelectedIndex;
            this.txtBreedNO.Text = this.grdList.Rows[index].Cells[1].Text;
            this.txtBreedName.Text = this.grdList.Rows[index].Cells[2].Text;
            GridViewRow gvr = grdList.Rows[index];
            Label lbStatus = (Label)gvr.FindControl("lbStatus");
            if (lbStatus.Text=="显示")
            {
                this.ckbHidden.Checked = false;
            }
            else
            {
                this.ckbHidden.Checked = true;
            }
            Session["BreedName"] = txtBreedName.Text;
            this.Panel1.Visible = false;
            this.Panel2.Visible = true;
        }

        public String GetCloseShow(int closeshow)
        { 
            if (closeshow==0)
            {
                return "显示";
            }
            else
            {
                return "关闭";
            }
        }
    }
}
